/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.own.entity.OwnMedicineFieldManagement;
import com.jeesite.modules.own.service.OwnMedicineFieldManagementService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 田间管理信息Controller
 * @author F
 * @version 2019-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/own/ownMedicineFieldManagement")
public class OwnMedicineFieldManagementController extends BaseController {

	@Autowired
	private OwnMedicineFieldManagementService ownMedicineFieldManagementService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OwnMedicineFieldManagement get(String id, boolean isNewRecord) {
		return ownMedicineFieldManagementService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("own:ownMedicineFieldManagement:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwnMedicineFieldManagement ownMedicineFieldManagement, Model model) {
		model.addAttribute("ownMedicineFieldManagement", ownMedicineFieldManagement);
		return "modules/own/ownMedicineFieldManagementList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineFieldManagement:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OwnMedicineFieldManagement> listData(OwnMedicineFieldManagement ownMedicineFieldManagement, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineFieldManagement.setPage(new Page<>(request, response));
		Page<OwnMedicineFieldManagement> page = ownMedicineFieldManagementService.findPage(ownMedicineFieldManagement);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("own:ownMedicineFieldManagement:view")
	@RequestMapping(value = "form")
	public String form(OwnMedicineFieldManagement ownMedicineFieldManagement, Model model) {
		model.addAttribute("ownMedicineFieldManagement", ownMedicineFieldManagement);
		return "modules/own/ownMedicineFieldManagementForm";
	}

	/**
	 * 保存田间管理信息
	 */
	@RequiresPermissions("own:ownMedicineFieldManagement:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OwnMedicineFieldManagement ownMedicineFieldManagement) {
		ownMedicineFieldManagement.setSpecificType(ownMedicineFieldManagement.getSpecificType().replaceAll(",", ""));
		ownMedicineFieldManagementService.save(ownMedicineFieldManagement);
		return renderResult(Global.TRUE, text("保存田间管理信息成功！"));
	}
	
	/**
	 * 删除田间管理信息
	 */
	@RequiresPermissions("own:ownMedicineFieldManagement:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OwnMedicineFieldManagement ownMedicineFieldManagement) {
		ownMedicineFieldManagementService.delete(ownMedicineFieldManagement);
		return renderResult(Global.TRUE, text("删除田间管理信息成功！"));
	}

	/**
	 * 根据种植信息id查询是否施肥以及是否喷药
	 */
	@RequiresPermissions("own:ownMedicineFieldManagement:view")
	@RequestMapping(value = "findByType")
	public Map<String, Object> findByType(String plantId) {
		Map<String, Object> re = new HashMap<>();
		re.put("success", false);re.put("py", false); re.put("sf", false);re.put("msg", "");
		try {
			List<OwnMedicineFieldManagement> ownMedicineFieldManagement = ownMedicineFieldManagementService.findByPlantAndType(plantId, "喷药");
			if (ownMedicineFieldManagement != null && ownMedicineFieldManagement.size() > 0){
				re.put("py", true);
			}
			List<OwnMedicineFieldManagement> ownMedicineFieldManagement1 = ownMedicineFieldManagementService.findByPlantAndType(plantId, "施肥");
			if (ownMedicineFieldManagement1 != null && ownMedicineFieldManagement1.size() > 0){
				re.put("sf", true);
			}
			re.put("success", true);
			re.put("msg", "获取成功！");
		}catch (Exception e){
			re.put("success", false);
			re.put("msg", "获取失败！"+e.getMessage());
		}finally {
			return re;
		}
	}
	
}